// 成果列表页面

import './index.less';
import {View} from "@tarojs/components";
import {useEffect, useState} from "react";
import {wxActions} from "../../utils/wxActions";
import {Api} from "../../api/api";
import Taro, {useReachBottom} from "@tarojs/taro";
import {AchList, SearchCount} from "../../components/achCmpt";

const TRACK_ARR = [
    "集成电路", "工业母机", "新一代移动通信", "工业软件", "人工智能", "生物技术", "新能源", "新能源汽车", "新材料"
];

export default function AchSub(props) {
    const [achList, setAchList] = useState([]); // 成果列表
    const [pageIndex, setPageIndex] = useState(1); // 页码
    const [total, setTotal] = useState(0); // 总数
    const [itIdsSave, setItIdsSave] = useState(""); // 赛道

    useEffect(() => {
        const fieldId = wxActions.getCurrentPageParams("fieldId");
        Api.getListIt().then(res => {
            const trackName = TRACK_ARR[fieldId - 1];
            const selectTrack = res.filter(item => item.itName === trackName)[0];
            const its = selectTrack.itId;
            setItIdsSave(its);
            const itsName = selectTrack.itName;
            Taro.setNavigationBarTitle({
                title: itsName
            })
            wxActions.showLoading('加载中');
            Api.getAchList({
                pageSize: 20,
                pageNum: 1,
                proName: "",
                itIds: its,
                stages: ""
            }).then(res => {
                setAchList(res.rows);
                setTotal(res.total);
            }).finally(() => {
                Taro.hideLoading();
            });
        })
    }, [])

    // 下触底事件
    useReachBottom(() => {
        if (achList.length >= total) {
            return;
        } else {
            wxActions.showLoading('加载中');
            Api.getAchList({
                pageSize: 20,
                pageNum: pageIndex + 1,
                proName: "",
                itIds: itIdsSave,
                stages: ""
            }).then(res => {
                setAchList([...achList, ...res.rows]);
                setPageIndex(pageIndex + 1);
            }).finally(() => {
                Taro.hideLoading();
            });
        }
    })

    return (
        <View className="achSub">
            {/*<View className={"count_row"}>*/}
            {/*    {*/}
            {/*        total > 0 && <SearchCount count={total}/>*/}
            {/*    }*/}
            {/*</View>*/}
            <View className={"achList_row"}>
                <AchList
                    achList={achList}
                />
            </View>
        </View>
    )
}
